

#ifndef BINARY_NODE_INCLUDED
#define BINARY_NODE_INCLUDED

#define MSVC_2010_FIX 1


class BinaryNode
{
public:
	static inline int CenterCount( int depth ) { return  1<<depth;/*2^n*/ }
	static inline int CornerCount( int depth ) { return (1<<depth)+1; }
	static inline int CumulativeCenterCount( int maxDepth ) { return (1<<(maxDepth+1))-1; }
	static inline int CumulativeCornerCount( int maxDepth ) { return (1<<(maxDepth+1))+maxDepth; }
	static inline int CenterIndex( int depth , int offSet ) { return (1<<depth)+offSet-1; }
	static inline int CornerIndex( int depth , int offSet ) { return (1<<depth)+offSet+depth; }

	static inline int CornerIndex( int maxDepth , int depth , int offSet , int forwardCorner ){ return (offSet+forwardCorner)<<(maxDepth-depth); }
	template< class Real > static inline Real CornerIndexPosition(int index,int maxDepth){ return Real(index)/(1<<maxDepth); }
	template< class Real > static inline Real Width(int depth){ return Real(1.0/(1<<depth)); }
	template< class Real > static inline void CenterAndWidth( int depth , int offset , Real& center , Real& width )
	  {
	    width=Real (1.0/(1<<depth) );
	    center=Real((0.5+offset)*width);
	  }
	template< class Real > static inline void CenterAndWidth( int idx , Real& center , Real& width )
	  {
	    int depth , offset;
	    DepthAndOffset( idx , depth , offset );
	    CenterAndWidth( depth , offset , center , width );
	  }
	static inline void DepthAndOffset( int idx , int& depth , int& offset )
	  {
	    int i=idx+1;
#if MSVC_2010_FIX
		depth = 0;
#else // !MSVC_2010_FIX
	    depth = -1;
#endif // MSVC_2010_FIX
	    while( i )
		{
	      i >>= 1;
	      depth++;
	    }
#if MSVC_2010_FIX
		depth--;
#endif // MSVC_2010_FIX
	    offset = ( idx+1 ) - (1<<depth);
	  }
};
#endif // BINARY_NODE_INCLUDED
